package com.example.nonameaa.controller;


import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.nonameaa.Common.Result;
import com.example.nonameaa.entity.Category;
import com.example.nonameaa.service.CategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author NoName
 * @since 2024-10-15
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Resource
    private CategoryService categoryService;

    /**
     *修改信息
     * @param category
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Category category) {
        categoryService.updateById(category);
        return Result.success();
    }

    /**
     * 删除信息
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        
        categoryService.removeById(id);
        return Result.success();
    }

    /**
     * 新增信息
     * @param category
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Category category) {

        categoryService.save(category);
        return Result.success();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids) {
        categoryService.removeByIds(ids);
        return Result.success();
    }

    /**
     * 查询全部信息
     * @return
     */
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Category> userList = categoryService.list(new QueryWrapper<Category>().orderByDesc("id"));
        return Result.success(userList);
    }

}
